<template>
    <div class="mainDiv">
        <el-row :gutter="20" class="rowHead">
            <el-col :span="12">
                <el-button-group>
                    <el-button type="success" @click="addcard(1)" v-qx="quanxian.creatone">创建期卡</el-button>
                    <el-button type="primary" @click="addcard(2)" v-qx="quanxian.creattwo">创建疗程卡</el-button>
                </el-button-group>
                <el-select v-model="page.zt" @change="reupdate" v-qx="quanxian.status">
                    <el-option v-for="o in leaves" :label="o.txt" :value="o.val"></el-option>
                </el-select>
            </el-col>
            <el-col :span="9">
                <el-input placeholder="搜索卡名称、卡编号" v-model="page.search" @keyup.enter.native="reupdate">
                    <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="reupdate"></i>
                </el-input>
            </el-col>
            <el-col :span="3">
                <el-button type="primary" icon="el-icon-h-shuaxin1" style="float:right" @click="refresh">刷新列表</el-button>
            </el-col>
        </el-row>
        <el-table :data="list" :stripe="true" height="100%">
            <el-table-column prop="no" label="#"></el-table-column>
            <el-table-column prop="name" label="卡名称" show-overflow-tooltip></el-table-column>
            <el-table-column prop="number" label="卡编号"></el-table-column>
            <el-table-column prop="category" label="卡类型">
                <template slot-scope="scope">
                    <Tags :typeValue="scope.row.category" typeName="cardType"></Tags>
                </template>
            </el-table-column>
            <el-table-column prop="price" label="卡价格">
                <template slot-scope="scope">
                    {{scope.row.price+'元'}}
                </template>
            </el-table-column>
            <el-table-column prop="validity" label="有效期">
                <template slot-scope="scope">
                    <el-tag color="#e189e8" style="color:#fff" size="mini" v-if="scope.row.validity != 0">{{scope.row.validity+'天'}}</el-tag>
                    <el-tag color="#78a4d8" style="color:#fff" size="mini" v-else>项目用完</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="remarks" label="卡描述" show-overflow-tooltip></el-table-column>
            <el-table-column prop="addtime" label="创建时间" show-overflow-tooltip>
                <template slot-scope="scope">{{scope.row.addtime|dateTime}}</template>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                    <el-button-group v-if="!scope.row.isdisabled">
                        <el-button type="primary" icon="el-icon-h-chakan" size="mini" @click="look(scope.row.id)" v-qx="quanxian.look"></el-button>
                        <el-button type="warning" icon="el-icon-h-edit" size="mini" @click="edit(scope.row)" v-qx="quanxian.edit"></el-button>
                        <el-tooltip content="停用当前会员卡" placement="top" :open-delay="500" v-qx="quanxian.unstop">
                            <el-button type="danger" icon="el-icon-h-jinyong" size="mini" @click="unstop(scope.row.id,1)"></el-button>
                        </el-tooltip>
                    </el-button-group>
                    <el-tooltip content="启用当前会员卡" placement="left" :open-delay="500" v-else v-qx="quanxian.unstop">
                        <el-button type="success" icon="el-icon-h-qiyong2" size="mini" @click="unstop(scope.row.id,0)"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <div class="block center bg-light">
            <el-pagination @size-change="sizeChange" @current-change="currentChange" :page-sizes="[15, 30, 50, 100]" :page-size="page.limits" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import C from '#/src/common/common.js'
import Tags from '../../../../components/orderTags.vue'
import Card from '#/src/shop/card.js'
export default {
    name: 'cardList',
    data() {
        return {
            list: [],
            page: {
                pages: 1,
                limits: 15,
                search: '',
                zt: 0,
            },
            total: 0,
            leaves: [{
                txt: '正常',
                val: 0
            }, {
                txt: '停用',
                val: 1
            }],
            quanxian: {
                creatone: 'cards_creatone',
                creattwo: 'cards_creattwo',
                unstop: 'cards_unstop',
                status: 'cards_status',
                edit: 'cards_edit',
                look: 'cards_look',
            }
        }
    },
    methods: {
        addcard: async function(tp) {
            let res = await C.openBrowserWindow('/addCard', { type: tp }, { width: 1400, height: 860, useSpa: true, spaModel: 'shop', transparent: true, frame: false })
            if (res) {
                C.success(res.data)
                this.reupdate()
            }
        },
        look: function(id) {
            Card.openCardInfo(id)
        },
        edit: async function(row) {
            let res = await C.openBrowserWindow('/addCard', { type: row.category, id: row.id }, { 
                width: 1400,
                height: 860,
                useSpa: true,
                spaModel: 'shop',
                transparent: true,
                frame: false,
                // openDevTools: true
            })
            if (res) {
                C.success(res.data)
                this.reupdate()
            }
        },
        unstop: async function(id, sta) {
            let str = ''
            if (sta) str = '确定要停用该卡？'
            else str = '是否启用当前卡？'
            C.confirm(str, async() => {
                let res = await C.ajax('/shop/card/unstop', { id: id, zt: sta })
                if (res) {
                    C.success(res.data)
                    this.reupdate()
                }
            })
        },
        refresh: function() {
            window.location.reload()
        },

        sizeChange: function(val) {
            this.page.limits = val
            this.reupdate()
        },
        currentChange: function(val) {
            this.page.pages = val
            this.reupdate()
        },
        reupdate: async function() {
            let data = await C.ajax('/shop/card/cardlist', this.page, 'post')
            if (data) {
                this.total = data.count
                this.list = data.data
            }
        }
    },
    mounted: function() {
        this.reupdate()
    },
    components: { Tags }
}
</script>